﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace LibExtend.GlassHelpers
{
    public class Drawing
    {
        private int m_Opacity = 25;
        public int Opacity 
        {
            get { return m_Opacity; }
            set {
                if( value > 255 ) value = 255;
                if (value < 0) value = 0;
                m_Opacity = value; }
        }

        private Color m_GlassColor = Color.WhiteSmoke;
        public Color GlassColor
        {
            get { return m_GlassColor; }
            set { m_GlassColor = value; }
        }

        public Rectangle m_Bounds = new Rectangle();
        public Rectangle Bounds
        {
            get { return m_Bounds; }
            set { m_Bounds = value; }
        }

        private RectangleF EffectiveBounds
        {
            get
            {
                return new RectangleF(Bounds.X + 0, Bounds.Y + 0, Bounds.Width - 4, Bounds.Height - 4);
            }
        }

        private int m_Radius = 5;
        public int Radius
        {
            get { return m_Radius; }
            set { m_Radius = value; }
        }

        private GraphicsPath RoundSurface()
        {
            return Helpers.RoundCorners(EffectiveBounds, Radius, Helpers.Corner.All);
        }

        private GraphicsPath RoundSurfaceInner()
        {
            RectangleF rect = EffectiveBounds;
            rect.Inflate(-1, -1);
            return Helpers.RoundCorners(rect, Radius, Helpers.Corner.All);
        }

        private Region InnerRegion()
        {
            Region rgInnerRegion = new Region(RoundSurface());
            return rgInnerRegion;
        }

        private Region OuterRegion()
        {
            Region rgOuterRegion = new Region(RoundSurface());
            rgOuterRegion.Xor(Bounds);
            return rgOuterRegion;
        }

        public void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            SolidBrush brGlass = new SolidBrush(Color.FromArgb(Helpers.Check(Opacity), GlassColor.R, GlassColor.G, GlassColor.B));
            //e.Graphics.FillPath(brGlass, RoundSurface());
            //PaintHorizontalSurface(null, e);
            //PaintGlowSurface(null, e);
            //PaintReflectiveBands(null, e);
            //PaintLightSource(null, e);
            e.Graphics.DrawPath(Pens.Gray, RoundSurface());
            brGlass.Dispose();
        }

        private void PaintReflectiveBands(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            SolidBrush brGlassReflect = new SolidBrush(Color.FromArgb(Helpers.Check((int)(Opacity * 0.5)), GlassColor.R, GlassColor.G, GlassColor.B));
            GraphicsPath grpBand1=CreateReflectiveBand(0.1F, 0.5F, 0.15F);
            GraphicsPath grpBand2 = CreateReflectiveBand(0.4F, 0.8F, 0.1F);
            e.Graphics.FillPath(brGlassReflect, grpBand1);
            e.Graphics.FillPath(brGlassReflect, grpBand2);

            brGlassReflect.Dispose();
        }

        private void PaintHorizontalSurface(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            SolidBrush brGlassDark = new SolidBrush(Color.FromArgb((int)(Opacity * 2.5F), Helpers.DeductMinZero(GlassColor.R, 50), Helpers.DeductMinZero(GlassColor.G, 50), Helpers.DeductMinZero(GlassColor.B, 50)));
            e.Graphics.ExcludeClip(new Rectangle((int)EffectiveBounds.Left, (int)EffectiveBounds.Top, (int)EffectiveBounds.Width, (int)(EffectiveBounds.Height * 0.66F)));
            e.Graphics.FillPath(brGlassDark, RoundSurface());
            e.Graphics.ResetClip();

            brGlassDark.Dispose();
        }
   
        private void PaintGlowSurface(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            LinearGradientBrush brGlassDarkLinear = new LinearGradientBrush(
                Bounds,
                Color.FromArgb(0, Helpers.DeductMinZero(GlassColor.R, 50), Helpers.DeductMinZero(GlassColor.G, 50), Helpers.DeductMinZero(GlassColor.B, 50)),
                Color.FromArgb(Helpers.Check((int)(Opacity * 2.5F)), GlassColor.R, GlassColor.G, GlassColor.B),
                LinearGradientMode.Vertical);

            e.Graphics.FillPath(brGlassDarkLinear, RoundSurfaceInner());

            brGlassDarkLinear.Dispose();
        }

        private GraphicsPath CreateReflectiveBand(Single LeftFactor, Single RightFactor, Single SizeFactor)
        {
            GraphicsPath grpBand = new GraphicsPath();
            grpBand.StartFigure();
            grpBand.AddLine(2 + (EffectiveBounds.Width * LeftFactor), 2, 2 + (EffectiveBounds.Width * LeftFactor) + (EffectiveBounds.Width * SizeFactor), 2);
            grpBand.AddLine((2 + (EffectiveBounds.Width * LeftFactor)) + (EffectiveBounds.Width * SizeFactor), 2, (2 + (EffectiveBounds.Width * RightFactor)) + (EffectiveBounds.Width * SizeFactor), EffectiveBounds.Top + EffectiveBounds.Height);
            grpBand.AddLine((2 + (EffectiveBounds.Width * RightFactor) + (EffectiveBounds.Width * SizeFactor)), 2 + EffectiveBounds.Height, EffectiveBounds.Left + (EffectiveBounds.Width * RightFactor), EffectiveBounds.Top + EffectiveBounds.Height);
            grpBand.AddLine(2 + (EffectiveBounds.Width * RightFactor), 2 + EffectiveBounds.Height, 2 + (EffectiveBounds.Width * LeftFactor), 2);
            grpBand.CloseFigure();

            return grpBand;
        }

        private void PaintLightSource(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            RectangleF rcLight = GetLightBounds(0.75F);
            GraphicsPath grpLight = new GraphicsPath();
            grpLight.StartFigure();
            grpLight.AddEllipse(rcLight);

            PathGradientBrush brLight = new PathGradientBrush(grpLight);
            brLight.CenterColor = Color.FromArgb(Helpers.Check(Opacity * 3), 255, 255, 255);
            brLight.SurroundColors = new Color[] {Color.FromArgb(0, 255, 255, 255)};

            e.Graphics.ExcludeClip(OuterRegion());
            e.Graphics.FillEllipse(brLight, rcLight);
            e.Graphics.ResetClip();

            brLight.Dispose();
        }
    
        private RectangleF GetLightBounds(Single Size)
        {
            return new RectangleF(2 - ((EffectiveBounds.Height * Size) / 2), 2 - ((EffectiveBounds.Height * Size) / 2), EffectiveBounds.Height * Size, EffectiveBounds.Height * Size);
        }

        private void PaintBorder(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawPath(new Pen(Color.FromArgb(200, 255, 255, 255), 0.5F), RoundSurface());
            e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 0, 0, 0), 0.5F), RoundSurfaceInner());
        }
    }
}
